package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.home.adapter.FreePaperAdapter;
import com.meitianhui.convenitenbuy.module.home.contract.FreePaperContract;
import com.meitianhui.convenitenbuy.module.home.di.DaggerHomeComponent;
import com.meitianhui.convenitenbuy.module.home.di.HomeModule;
import com.meitianhui.convenitenbuy.module.home.model.bean.FreePaper;
import com.meitianhui.convenitenbuy.module.home.presenter.FreePaperPresenter;
import com.meitianhui.convenitenbuy.module.wallet.model.PayResultBean;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/18.
 */

public class FreePaperActivity extends BaseToolbarActivity implements FreePaperContract.View {
    private RecyclerView mRecyclerView;
    private String mActivityId;
    private String mDiscountPrice;
    private String mJson;
    private FreePaperAdapter mAdapter;
    private int mPageIndex = 1;
    private int mPageSize = 15;
    private int mCheck=-1;
    private List<FreePaper.ListBean> paperList = new ArrayList<>();
    private View mNotDataView;
    private ImageView mIvBg;
    private TextView mTvContent;
    @Inject
    FreePaperPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_free_paper;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerHomeComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
        setTbTitle("免单券");
        mRecyclerView = getViewById(R.id.rv_free_paper);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        RxUtils.bindEvents(getViewById(R.id.tv_cancel), this);
        RxUtils.bindEvents(getViewById(R.id.tv_confirm), this);
        mActivityId = getIntent().getStringExtra("activity_id");
        mDiscountPrice = getIntent().getStringExtra("discount_price");
        mJson = getIntent().getStringExtra("json");
        mNotDataView = getLayoutInflater().inflate(R.layout.data_empty_goods, (ViewGroup) mRecyclerView.getParent(), false);
        mIvBg = (ImageView) mNotDataView.findViewById(R.id.iv_bg);
        mTvContent = (TextView) mNotDataView.findViewById(R.id.tv_content);
        mIvBg.setBackgroundResource(R.mipmap.icon_empty_coupon);
        mTvContent.setText("没有免单券");
        mAdapter = new FreePaperAdapter(this,null,mDiscountPrice);
        mRecyclerView.setAdapter(mAdapter);
        onRefreshData();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(StringUtils.moneyComp(paperList.get(position).getLimited_price(),mDiscountPrice)){
                    mCheck = position;
                    mAdapter.setCheck(position);
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_confirm:
                if(mCheck==-1){
                    TipUtils.showToast(App.getInstance(),"请选择免单券",3);
                    return;
                }
                if(paperList.size()>0) {
                   presenter.joinFightGroupPay(mActivityId,paperList.get(mCheck).getBenefit_id(),mDiscountPrice);
                }
                break;


        }
    }

    @Override
    protected void onRefreshData() {
        mPageIndex = 1;
        presenter.getFreePaper(mPageIndex+"",mPageSize+"");
    }



    @Override
    public void getFreePaper(FreePaper data) {
        refreshFinish();
        setData(mPageIndex,data.getList());
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPageIndex++;
                presenter.getFreePaper(mPageIndex+"",mPageSize+"");
            }
        },mRecyclerView);
    }

    @Override
    public void joinFightGroupPaySuccess() {
        RxBus.get().post(new PayResultBean(Constants.PAY_FIGHT_GROUP_SUCCESS));
        Intent intent = new Intent(FreePaperActivity.this,FightGroupShareActivity.class);
        intent.putExtra("type","join_fight");
        intent.putExtra("json",mJson);
        startActivity(intent);
        finish();
    }

    @Override
    public void joinFightGroupPayFail(int code, String msg) {
        TipUtils.showToast(App.getInstance(),msg);
    }


    private void setData(int pageIndex, List data) {
        final int size = data == null ? 0 : data.size();
        if(pageIndex==1){
            mAdapter.setNewData(data);
            if(size==0){
                mAdapter.setEmptyView(mNotDataView);
            }
        }else{
            if(size>0){
                mAdapter.addData(data);
            }
        }
        if (size < mPageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(false);
        } else {
            mAdapter.loadMoreComplete();
        }
        paperList = mAdapter.getData();
    }

    @Override
    public void showError(String msg) {

    }

}
